package club.xinghe.biz;

import club.xinghe.dao.ExamDao;
import club.xinghe.entity.Exam;
import club.xinghe.entity.Tk;
import club.xinghe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说明:对试卷进行操作的业务
 *
 * @姚文斌
 * @create 2019-09-28 下午 6:23
 */
public class ExamBiz {
    SqlSession session= SqlSessionFactoryUtil.getSqlSession();
    ExamDao examDao=session.getMapper(ExamDao.class);
    //1.由专业id和年级id去查询试卷
    public List<Exam> getExamByZidGid(int gid,int zid){

        return examDao.getExamByZidGid(gid,zid);
    }
    /*
     * 全部显示
     * */
    /**
     * 返回该题库中一套试题的全部信息和考试的该张试卷的信息
     * @param eid
     * @return
     */
    public Map<String,Object> stuExamBiz(int eid){
        Map<String,Object> hashMap=new HashMap<>();
        ArrayList<Tk> tkArrayList=new ArrayList<>();//集合用于存放考试试题
        Exam exam=null;
        Tk onetk=null;
        exam=examDao.stuExam(eid);//获得考试信息
        String str=exam.geteString();//获得试卷的所有题的id
        String strArr[]=str.split(",");//分割成字符数组
        for(String si:strArr){
            int tid=Integer.parseInt(si);//遍历得到每一个题的id，并转为int型
            onetk=examDao.selectTK(tid);//通过每一题的id在题库查到每一题的具体内容
//            System.out.print(onetk.gettId()+","+onetk.gettContent()+",");
//            System.out.println(onetk.getStyle().getStyleId());
            tkArrayList.add(onetk);
        }
        hashMap.put("tkArrayList",tkArrayList);//每一题的具体信息的集合存入map集合
        hashMap.put("exam",exam);//存入考试信息
        return hashMap;
    }
    /*
     *
     * 分页显示
     * */

    /**
     * 获取总行数，作为总页数
     * @param eid
     * @return
     */
    public int stuExamPageCountRowBiz(int eid){
        Exam exam=examDao.stuExam(eid);//获得考试信息
        String str=exam.geteString();//获得试卷的所有题的id
        String strArr[]=str.split(",");//分割成字符数组
        return strArr.length;
    }

    /**
     * 根据试卷的id和要显示的当前页
     * 返回该题库中该试题的全部信息
     * @param eid
     * @param currPage
     * @return
     */
    public Tk stuExamCurrentPageBiz(int eid,int currPage){
        Map<String,Object> hashMap=new HashMap<>();
        Exam exam=examDao.stuExam(eid);//获得考试信息
        String str=exam.geteString();//获得试卷的所有题的id
        String strArr[]=str.split(",");//分割成字符数组
        int tid=Integer.parseInt(strArr[currPage-1]);//遍历得到当前题的id，并转为int型
        Tk onetk=examDao.selectTK(tid);//通过该题的id在题库查到具体内容
        return onetk;
    }

    /**
     * 根据试卷id查询整张试题的信息
     * @param eid
     * @return
     */
    public Exam stuShowExamBiz(int eid){
        return examDao.stuExam(eid);//获得考试信息
    }

    /*
     *查询答案
     **/
    /**
     * 根据tid获取tanswer,查询答案，返回答案集的数组
     * @param eid
     * @return
     */
    public String[] getOneAnswer(int eid){
        Exam exam=null;
        exam=examDao.stuExam(eid);//获得考试信息
        String str=exam.geteString();//获得试卷的所有题的id
        String strArr[]=str.split(",");//分割成字符数组
        String answer[]=new String[strArr.length];//设置接收正确答案的数组与题数的数组长度相同，返回答案数组
        for(int i=0;i<strArr.length;i++){
            int tid=Integer.parseInt(strArr[i]);//遍历得到每一个题的id，并转为int型
            answer[i]=examDao.getOneAnswer(tid).trim();//获取每一题的答案
        }
        return answer;
    }



    /**
     * 说明：计算试卷的数量
     * 姚家乐
     * @return
     */
    public int examCount(int zid,String see){
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        int count = 0;
        try {
            ExamDao examDao = session.getMapper(ExamDao.class);
            Map map = new HashMap();
            map.put("zid",zid);
            map.put("see",see);
            count = examDao.examCount(map);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return count;
    }

    /**
     * 说明：对卷库进行遍历展出
     * 姚家乐
     * @return
     */
    public List<Exam> Exam(String see,int pageNum,int pageSize,int zid){
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        List<Exam> list = new ArrayList<>();

        Map map = new HashMap();
        map.put("see",see);
        map.put("pageS",pageSize*(pageNum-1));
        map.put("pageSize",pageSize);
        map.put("zid",zid);

        try {
            ExamDao examDao = session.getMapper(ExamDao.class);
            list = examDao.eXam(map);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return list;
    }

    /**
     * 说明：删除指定ID的试卷
     * 姚家乐
     * @param eid
     * @return
     */
    public int delExam(int eid){
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        int n = 0;
        try {
            ExamDao examDao = session.getMapper(ExamDao.class);
            n = examDao.delExam(eid);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.commit();
            session.close();
        }
        return n;
    }

    /**
     * 说明：根据指定ID查看试卷
     * 姚家乐
     * @param eid
     * @return
     */
    public Exam selExzm(int eid){
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        Exam exam = new Exam();
        try {
            ExamDao examDao = session.getMapper(ExamDao.class);
            exam = examDao.selExzm(eid);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return exam;
    }

    /**
     * 说明：根据指定的ID修改试卷
     * 姚家乐
     * @return
     * */
    public int upExam(String estring,int eid,String ename,String estart,String eend,int etime,int gid){
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        int n = 0;
        try {
            ExamDao examDao = session.getMapper(ExamDao.class);
            Map map = new HashMap();
            map.put("estring",estring);
            map.put("eid",eid);
            map.put("ename",ename);
            map.put("estart",estart);
            map.put("eend",eend);
            map.put("etime",etime);
            map.put("gid",gid);
            n = examDao.upExam(map);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.commit();
            session.close();
        }
        return n;
    }

    /**
     * 说明：添加试卷
     * 姚家乐
     * */
    public int addExam(Map map){
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        int n = 0;
        try {
            ExamDao examDao = session.getMapper(ExamDao.class);
            n = examDao.addExam(map);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.commit();
            session.close();
        }
        return n;
    }


}
